{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook is intended to demonstrate how vessel segmentation methods of ITKTubeTK can be applied to multi-channel MRA."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import itk\n",
    "from itk import TubeTK as ttk\n",
    "\n",
    "from itkwidgets import view\n",
    "\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "ImageType = itk.Image[itk.F, 3]\n",
    "\n",
    "im1 = itk.imread(\"../Data/MRI-Cases/mra.mha\", itk.F)\n",
    "\n",
    "resampler = ttk.ResampleImage.New( Input=im1, MakeHighResIso=True )\n",
    "resampler.Update()\n",
    "im1iso = resampler.GetOutput()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#view(im1iso)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "imMath = ttk.ImageMath[ImageType,ImageType].New( Input=im1iso )\n",
    "imMath.Blur(1)\n",
    "imBlur = imMath.GetOutput()\n",
    "\n",
    "numSeeds = 40"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "vSeg = ttk.SegmentTubes[ImageType].New()\n",
    "vSeg.SetInput(im1iso)\n",
    "vSeg.SetVerbose(True)\n",
    "vSeg.SetMinCurvature(0.0)\n",
    "vSeg.SetMinRoundness(0.01)\n",
    "vSeg.SetRadiusInObjectSpace( 1 )\n",
    "vSeg.SetRadiusMin(1)\n",
    "vSeg.SetRadiusMax(8)\n",
    "vSeg.SetDataMinMaxLimits(50,256)\n",
    "vSeg.SetSeedMask( imBlur )\n",
    "vSeg.SetUseSeedMaskAsProbabilities(True)\n",
    "vSeg.SetSeedMaskMaximumNumberOfPoints( numSeeds )\n",
    "\n",
    "vSeg.ProcessSeeds()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "9ee5e8ef26c4406c800bfba5b1560441",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(geometries=[], gradient_opacity=0.22, interpolation=False, point_sets=[], rendered_image=<itk.itkImageP…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "tubeMaskImage = vSeg.GetTubeMaskImage()\n",
    "#view(tubeMaskImage)\n",
    "view(image=im1iso, label_image=tubeMaskImage)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "SOWriter = itk.SpatialObjectWriter[3].New(vSeg.GetTubeGroup())\n",
    "SOWriter.SetFileName( \"Demo-SegmentBrightVessels.tre\" )\n",
    "SOWriter.Update()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
